/**
 * 评论相关路由 - Express 路由
 */
import express from 'express'
import CommentHandler from '../handlers/comment.js'

const router = express.Router()

// 获取评论列表
router.get('/comments', (req, res) => {
  const response = CommentHandler.getComments(req.query)
  res.status(response.code).json(response)
})

// 获取评论详情
router.get('/comments/:id', (req, res) => {
  const response = CommentHandler.getCommentById(req.params.id)
  res.status(response.code).json(response)
})

// 创建评论
router.post('/comments', (req, res) => {
  const response = CommentHandler.createComment(req.body)
  res.status(response.code).json(response)
})

// 更新评论
router.put('/comments/:id', (req, res) => {
  const response = CommentHandler.updateComment(req.params.id, req.body)
  res.status(response.code).json(response)
})

// 删除评论
router.delete('/comments/:id', (req, res) => {
  const response = CommentHandler.deleteComment(req.params.id)
  res.status(response.code).json(response)
})

// 点赞/取消点赞评论
router.post('/comments/:id/like', (req, res) => {
  const { userId } = req.body
  const response = CommentHandler.toggleLike(req.params.id, userId)
  res.status(response.code).json(response)
})

// 获取评论的回复列表
router.get('/comments/:id/replies', (req, res) => {
  const response = CommentHandler.getReplies(req.params.id, req.query)
  res.status(response.code).json(response)
})

// 搜索用户（用于@功能）
router.get('/users/search', (req, res) => {
  const response = CommentHandler.searchUsers(req.query)
  res.status(response.code).json(response)
})

// 获取评论统计信息
router.get('/articles/:articleId/comments/stats', (req, res) => {
  const response = CommentHandler.getCommentStats(req.params.articleId)
  res.status(response.code).json(response)
})

export default router
